package com.rich.dmr.receiver;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.util.Log;

import com.rich.dmr.service.DmrService;
import com.rich.dmr.ui.activity.MainActivity;
import com.rich.dmr.utils.SoundPoolUtils;
import java.util.List;
import rich.dmr.DmrManager;
public class DmrBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        @SuppressLint("WrongConstant")
        DmrManager mDmrManager = (DmrManager) context.getSystemService("dmrservice");
        SharedPreferences mSp = context.getSharedPreferences("rich_dmr", Context.MODE_PRIVATE);
        long timeDown = mSp.getLong("timeDown", System.currentTimeMillis() - 2000);
        //timeUp = mSp.getLong("timeUp", System.currentTimeMillis() - 2000);
        long temp = System.currentTimeMillis();
        String TAG = "rich---DmrBroadcastReceiver";
        if ("android.intent.action.PTT.down".equals(intent.getAction())) {
            if (mDmrManager.getMode() == 0) {
                if (temp - timeDown > 800) {
                    Log.d(TAG, "android.intent.action.PTT.down");
                    /*if (!isAppIsInBackground(context)) {
                        //mDmrManager.sendDmrPTT(true);
                        //abortBroadcast();
                        Log.d(TAG, "isAppIsInBackground false");
                    } else {
                        Log.d(TAG, "isAppIsInBackground true");
                        if (isMobileDataEnable(context) || isWifiDataEnable(context)) {
                            Log.d(TAG, "network available.");
                        } else {
                            Log.d(TAG, "network unavailable.");
                            //mDmrManager.sendDmrPTT(true);
                            //abortBroadcast();
                        }
                    }*/
                    mDmrManager.sendDmrPTT(true);
                    abortBroadcast();
                } else {
                    SoundPoolUtils.getInstance(context).playSoundErr();
                    Log.d(TAG, "SoundPoolUtils down");
                }
                timeDown = System.currentTimeMillis();
                SharedPreferences.Editor mEditor = mSp.edit();
                mEditor.putLong("timeDown", timeDown);
                mEditor.apply();
            }

        } else if ("android.intent.action.PTT.up".equals(intent.getAction())) {
            if (mDmrManager != null) {
                Log.d(TAG, "android.intent.action.PTT.up");
                    /*if (!isAppIsInBackground(context)) {
                        //mDmrManager.sendDmrPTT(false);
                        //abortBroadcast();
                        Log.d(TAG, "isAppIsInBackground false");
                    } else {
                        Log.d(TAG, "isAppIsInBackground true");
                        if (isMobileDataEnable(context) || isWifiDataEnable(context)) {
                            Log.d(TAG, "network available.");
                        } else {
                            Log.d(TAG, "network unavailable.");
                            //mDmrManager.sendDmrPTT(false);
                            //abortBroadcast();
                        }
                    }*/
                if (mDmrManager.getMode() == 0) {
                    mDmrManager.sendDmrPTT(false);
                    abortBroadcast();
                }
            }
        }else if("android.intent.action.KNOB_LONG_PRESS".equals(intent.getAction())){
            if(isAppIsInBackground(context)){
                Log.d(TAG, "android.intent.action.KNOB_LONG_PRESS");
                intent.setClass(context,MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        }else if("android.intent.action.KNOB_TRIPLE_PRESS".equals(intent.getAction())){
                Log.d(TAG, "android.intent.action.KNOB_TRIPLE_PRESS");
                mDmrManager.setMode(1);
                System.exit(0);
        }
    }

    private boolean isAppIsInBackground(Context context) {
        boolean isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
        return isInBackground;
    }

    public static boolean isMobileDataEnable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean isMobileDataEnable = false;
        isMobileDataEnable = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        return isMobileDataEnable;
    }


    public static boolean isWifiDataEnable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean isWifiDataEnable = false;
        isWifiDataEnable = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        return isWifiDataEnable;
    }


}
